/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.sys.scheduler;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.sys.entity.Config;
import com.boxin.ims.modules.sys.service.ConfigService;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.boxin.ims.modules.wechat.web.WechatMemberController;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;

/**
 * 全局配置Controller
 * @author Jakemanse
 * @version 2013-07-31
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/sys/scheduler")
public class SchedulerController extends BaseController  {

	@Autowired
	private ConfigService configService;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private WechatDaoHelper wechatDaoHelper;
	
	private final Logger logger = Logger
			.getLogger(SchedulerController.class);
	
	@ModelAttribute
	public Config get(@RequestParam(required=false) Long id) {
		if (id != null){
			return configService.get(id);
		}else{
			return new Config();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Config config, HttpServletRequest request, HttpServletResponse response, Model model) {
			
		Map<String, String>	params=new HashMap<String, String>();
		
		params.put("valid", "1");
		
		List<Map<String, Object>>  list= wechatDaoHelper.queryAllSchedulers(params);
        model.addAttribute("list", list);
		return "modules/sys/schedulerList";
	}

	
	@RequestMapping(value = "start")
	public String start(String name, RedirectAttributes redirectAttributes) {
		AbstractScheduler scheduler = (AbstractScheduler) applicationContext.getBean(name);
		Map<String,String> params = new HashMap<String,String>();
		params.put("name", name);
		params.put("is_valid", "1");
		SchedulerInfo schedulerInfo = wechatDaoHelper.findSchedulerInfo(params);
		scheduler.setSchedulerInfo(schedulerInfo);
		scheduler.start();
		scheduler.process();
		logger.debug("开始启动【"+name+"】定时器...");
		addMessage(redirectAttributes, "启动成功");
		return "redirect:"+Global.ADMIN_PATH+"/sys/scheduler/";
	}
	
	@RequestMapping(value = "stop")
	public String stop(String name, RedirectAttributes redirectAttributes) {
		AbstractScheduler scheduler = (AbstractScheduler) applicationContext.getBean(name);
		Map<String,String> params = new HashMap<String,String>();
		params.put("name", name);
		params.put("is_valid", "1");
		SchedulerInfo schedulerInfo = wechatDaoHelper.findSchedulerInfo(params);
		scheduler.setSchedulerInfo(schedulerInfo);
		scheduler.stop();
		logger.debug("关闭【"+name+"】定时器...");
		addMessage(redirectAttributes, "关闭成功");
		return "redirect:"+Global.ADMIN_PATH+"/sys/scheduler/";
	}
	
	
	
	@RequestMapping(value = "updateMemberLottery")
	public String updateMemberLottery(String name, RedirectAttributes redirectAttributes,HttpServletRequest request, Model model) {
		
		String update_time=request.getParameter("update_time");
		if(update_time!=null){
			Map<String , Object> params =new HashMap<String, Object>();
			 params.put("update_time", update_time);
			wechatDaoHelper.cleanMemberLotteryByToday(params);
			//1.核销    LotteryRecord  奖品
			  
			  Map<String , Object> params1 =new HashMap<String, Object>();
			  
			  params1.put("award_type", "会员抽奖");
			  params1.put("source_type", 1);
			  params1.put("update_time", update_time);
			wechatDaoHelper.queryLotteryRecordBYSourceType(params1);
			Map<String , Object> params2 =new HashMap<String, Object>();
			params2.put("award_type", "商家抽奖");
			params2.put("source_type", 2);
			 params2.put("update_time", update_time);
			wechatDaoHelper.queryLotteryRecordBYSourceType(params2);
			
			  Map<String , Object> params3 =new HashMap<String, Object>();
			params3.put("award_type", "会员注册礼品");
			params3.put("source_type", 3);
			 params3.put("update_time", update_time);
			wechatDaoHelper.queryLotteryRecordBYSourceType(params3);
			
			
			
			//2.核销    积分兑换 exchangeRecord
			  Map<String , Object> params4 =new HashMap<String, Object>();
			  params4.put("update_time", update_time);
			wechatDaoHelper.queryExchangeRecord(params4);
			//3.核销    转发朋友圈  		ShareCircle
			  Map<String , Object> params5 =new HashMap<String, Object>();
			  params5.put("update_time", update_time);
			wechatDaoHelper.queryShareBYWin(params5);
			
			//4.核销    秒杀    SeckillGainer	//
			  Map<String , Object> params6 =new HashMap<String, Object>();
			  params6.put("update_time", update_time);
			wechatDaoHelper.querySeckillGainer(params6);
			
			//5.核销优惠券  wp_Coupon
			  Map<String , Object> params7 =new HashMap<String, Object>();
			  params7.put("update_time", update_time);
			wechatDaoHelper.queryCoupon(params7);
			
			model.addAttribute("update_time", update_time);
			model.addAttribute("message","更新成功");
		}else{
			model.addAttribute("message","时间为空");
		}
		
		return "redirect:"+Global.ADMIN_PATH+"/sys/scheduler/toMemberLottery";
	}
	
	@RequestMapping(value = "toMemberLottery")
	public String toMemberLottery(String name, RedirectAttributes redirectAttributes,HttpServletRequest request, Model model) {
		
		return "modules/sys/memberLotteryForm";
	}


}
